<?php

header('Access-Control-Allow-Origin: *');

require_once('./wp-load.php');

$debug_info	= '';

$target_path = "uploads/";

$file_name	=	basename( $_FILES['file']['name']);
 
$target_path = $target_path . basename( $_FILES['file']['name']);
/*
直接上传到wordpress的上传目录

*/
$wp_upload_dir = wp_upload_dir();
$target_path	= $wp_upload_dir['path'].'/'. basename( $_FILES['file']['name']);
if (move_uploaded_file($_FILES['file']['tmp_name'], $target_path)) {
    echo "Upload and move success";
	
} else {
	echo $target_path;
    echo "There was an error uploading the file, please try again!";
}

$debug_info = $debug_info.'target_path:'.$target_path;
add_option('upload_php_debug',$debug_info);

/*
根据传递来的参数创建post

*/
$user_ID=1;
$new_post = array(
'post_title' => $_POST['post_title'],
'post_content' => $_POST['post_content'],
'post_status' => 'publish',
'post_date' => date('Y-m-d H:i:s'),
'post_author' => $user_ID,
'post_type' => 'post',
'post_category' => array(0)
);
$post_id = wp_insert_post($new_post);


//===================insert attachment=================================
// $filename should be the path to a file in the upload directory.
$filename = $target_path;

// The ID of the post this attachment is for.
$parent_post_id = $post_id;

// Check the type of file. We'll use this as the 'post_mime_type'.
$filetype = wp_check_filetype( basename( $filename ), null );

// Get the path to the upload directory.
$wp_upload_dir = wp_upload_dir();

$debug_info .=	'basename(filename):'.basename($filename);
$debug_info .=  'wp_upload_dir:'.$wp_upload_dir['url'];
update_option('upload_php_debug',$debug_info);
// Prepare an array of post data for the attachment.
$attachment = array(
	'guid'           => $wp_upload_dir['url'] . '/' . basename( $filename ), 
	'post_mime_type' => $filetype['type'],
	'post_title'     => preg_replace( '/\.[^.]+$/', '', basename( $filename ) ),
	'post_content'   => '',
	'post_status'    => 'inherit'
);

// Insert the attachment.
$attach_id = wp_insert_attachment( $attachment, $filename, $parent_post_id );

// Make sure that this file is included, as wp_generate_attachment_metadata() depends on it.
require_once( ABSPATH . 'wp-admin/includes/image.php' );

// Generate the metadata for the attachment, and update the database record.
$attach_data = wp_generate_attachment_metadata( $attach_id, $filename );
wp_update_attachment_metadata( $attach_id, $attach_data );

set_post_thumbnail( $parent_post_id, $attach_id );
/*
把图片作为post的封面图片

*/
/*
if( $image_name != '' && !has_post_thumbnail( $post_id ) ){
  $base = get_stylesheet_directory();
  $imgfile= $base . '/import/' . $image_name; // Images are stored inside an imports folder, in the theme directory
  $filename = basename($imgfile);
  $upload_file = wp_upload_bits($filename, null, file_get_contents($imgfile));
  if (!$upload_file['error']) {
    $wp_filetype = wp_check_filetype($filename, null );
    $attachment = array(
      'post_mime_type' => $wp_filetype['type'],
      'post_parent' => 0,
      'post_title' => preg_replace('/\.[^.]+$/', '', $filename),
      'post_content' => '',
      'post_status' => 'inherit'
    );
    $attachment_id = wp_insert_attachment( $attachment, $upload_file['file'], $post_id );

    if (!is_wp_error($attachment_id)) {
      require_once(ABSPATH . "wp-admin" . '/includes/image.php');
      $attachment_data = wp_generate_attachment_metadata( $attachment_id, $upload_file['file'] );
      wp_update_attachment_metadata( $attachment_id,  $attachment_data );
    }
    set_post_thumbnail( $post_id, $attachment_id );

  }
}
*/
?>